package com.oneToone.byFk.service;

import com.oneToone.byFk.model.Address;
import com.oneToone.byFk.model.Customer;
import com.utils.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class BusinessService {

    private HibernateUtil hibernateUtil = new HibernateUtil();
    private SessionFactory sessionFactory = hibernateUtil.getSessionFactory();

    private void saveCustomer(Customer customer) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(customer);
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
                System.out.println();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private Customer loadCustomer(Long id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Customer customer = (Customer) session.get(Customer.class, id);
            Hibernate.initialize(customer.getHomeAddress());
            Hibernate.initialize(customer.getComAddress());
            tx.commit();
            return customer;

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void printCustomer(Customer customer) {
        Address homeAddress = customer.getHomeAddress();
        Address comAddress = customer.getComAddress();
        System.out.println("Home Address of " + customer.getName() + " is: "
                + homeAddress.getProvince() + " "
                + homeAddress.getCity() + " "
                + homeAddress.getStreet());

        System.out.println("Company Address of " + customer.getName() + " is: "
                + comAddress.getProvince() + " "
                + comAddress.getCity() + " "
                + comAddress.getStreet());

        if (homeAddress.getCustomer() == null)
            System.out.println("Can not naviagte from homeAddress to Customer.");

        if (comAddress.getCustomer() == null)
            System.out.println("Can not naviagte from comAddress to Customer.");

    }

    private void test() {

        Customer customer = new Customer();
        Address homeAddress = new Address("province1", "city1", "street1", "100001", customer);
        Address comAddress = new Address("province2", "city2", "street2", "200002", customer);
        customer.setName("Tom");
        customer.setHomeAddress(homeAddress);
        customer.setComAddress(comAddress);

        saveCustomer(customer);
        customer = loadCustomer(customer.getId());
        printCustomer(customer);

        sessionFactory.close();

    }

    public static void main(String args[]) {
        new BusinessService().test();
    }
}

